<?php

	/**
	* @Name : 权限收集与判断侦听器
	* @Function : 
	* @Author : Haiwera
	* @Created : 2016-10-28
	* @Copyright : Haiwera.Fiidee.Monda.Cn
	*/

	namespace admin\listener;

	use herosphp\listener\WebApplicationListenerMatcher;
	use herosphp\core\WebApplication;
	use herosphp\bean\Beans;
	use herosphp\http\HttpRequest;

	class PrivilegeListener extends WebApplicationListenerMatcher{

		/**
		* 请求初始化之前
		* @return mixed
		*/
		public function beforeRequestInit()
		{
			// TODO: Implement beforeRequestInit() method.
		}

		/**
		* action 方法调用之前
		* @return mixed
		*/
		public function beforeActionInvoke(HttpRequest $request)
		{
			$app = WebApplication::getInstance();

			$request = $app->getHttpRequest();
			$service = Beans::get('admin.adminNode.service');
			$module = $request->getModule();
			$action = $request->getAction();
			$method = $request->getMethod();
			if(preg_match("/^[\w_]*$/",$method))
			{
				$key = strtolower($module . '/'.$action.'/'.$method);
				if(!$service->getItem(array('node_key' => $key)))
				{
					$data = array(
						'name' => $key,
						'node_key' => $key,
						'addtime' => date('Y-m-d H:i:s'),
						'edittime' => date('Y-m-d H:i:s'),
						'remark' => '自动添加'
					);
					$service->add($data);
				}
			}

		} 

		/**
		* 响应发送之前
		* @return mixed
		*/
		public function beforeSendResponse(HttpRequest $request,$actionInstance)
		{
			// TODO: Implement beforeSendResponse() method.
		}

		/**
		* 响应发送之后
		* @return mixed
		*/
		public function afterSendResponse($actionInstance)
		{
			// TODO: Implement afterSendResponse() method.
		}
	}
